//
//  ABTools.m
//  AirBox
//
//  Created by wangda on 15-3-17.
//  Copyright (c) 2015年 wangda. All rights reserved.
//

#import "ABTools.h"
#import "AFNetworkActivityLogger.h"
//#import "AMPopTip.h"
#define Degrees_To_Radians(x) (x * M_PI / 180)

#ifndef DEBUG_FLAG
#define DEBUG_AFNET 1
#endif
@implementation ABTools

+ (AFHTTPRequestOperationManager*)managerwithURL:(NSString *)url
{
    static AFHTTPRequestOperationManager* instance ;
    static dispatch_once_t onceToken;
    //如果ABTools是在并发情况下调用的方法，方法不会引发线程安全问题
    dispatch_once(&onceToken, ^{
        //创建单个实例
        instance = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:url]];
        //可以设置超时的时间
        instance.requestSerializer.timeoutInterval=30.0f;
        
        //设定提交的默认请求处理类为JSON格式提交
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        instance.responseSerializer = [AFJSONResponseSerializer serializer];
        //正常情况，服务器如果返回json，其头信息中会给出content-type，一般为text/json或者application/json
        //而当前服务器返回的头类型是text/html
        NSMutableSet *acceptTypes =  instance.responseSerializer.acceptableContentTypes.mutableCopy;
        

        [acceptTypes addObject:@"plain/text"];
        [acceptTypes addObject:@"json/application"];
        
        instance.responseSerializer.acceptableContentTypes = acceptTypes.copy;
        //增加请求响应的数据包调试
        if (DEBUG_AFNET) {
            [[AFNetworkActivityLogger sharedLogger]startLogging];
            [[AFNetworkActivityLogger sharedLogger]setLevel:AFLoggerLevelDebug];
        }
    });
    return instance;
}

+(void)pauseLayer:(UIView *)view
{
    CFTimeInterval pausedTime = [view.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    view.layer.speed = 0.0;
    view.layer.timeOffset = pausedTime;
}

+(void)resumeLayer:(UIView *)view
{
    CFTimeInterval pausedTime = [view.layer timeOffset];
    view.layer.speed = 1.0;
    view.layer.timeOffset = 0.0;
    view.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [view.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    view.layer.beginTime = timeSincePause;
}




//图片旋转开始
+(void) addAnimation1WithView:(UIView *)view
{
    CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation2.duration=5;
    animation2.repeatCount = 3;
    animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    NSMutableArray *values = [NSMutableArray array];
    //[values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(0)]];
    [values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(45)]];
    [values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(0)]];
    [values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(-45)]];
    [values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(0)]];
    [values addObject:[NSNumber numberWithFloat:Degrees_To_Radians(45)]];
    animation2.values = values;
    
    CGPoint oldAnchorPoint = view.layer.anchorPoint;
    [view.layer setAnchorPoint:CGPointMake(0.5, 0)];
    [view.layer setPosition:CGPointMake(view.layer.position.x + view.layer.bounds.size.width * (view.layer.anchorPoint.x - oldAnchorPoint.x), view.layer.position.y + view.layer.bounds.size.height * (view.layer.anchorPoint.y - oldAnchorPoint.y))];
    
    [view.layer addAnimation:animation2 forKey:@"animation"];
}

//图片旋转开始
+(void) addAnimationWithView:(UIView *)view
{
    //添加旋转动画
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = NO;
    rotationAnimation.repeatCount = 100000;
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self resumeLayer:view];
}

+(void)stopAnimationWithView:(UIView *)view
{
    [self pauseLayer:view];
    [view.layer removeAnimationForKey:@"rotationAnimation"];
}

+(void)resumeAnimationWithView:(UIView *)view
{
    
    [self resumeLayer:view];
}


+(void)setUserDefault:(id)object key:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(id)getUserDefault:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

+(void)removeUserDefault:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}


+(NSString *)getUsefulStringWithString:(NSString *)string
{
    NSArray *arr = [string componentsSeparatedByString:@"("];
    
    return [arr[1] componentsSeparatedByString:@")"][0];
}

//手机震动
+(void)vibrate
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}







+(NSString*) getText:(NSString *)msgkey
{
    //NSLo
    return nil;
}

+(NSDictionary*)fetchSSIDInfo {
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
     id info = nil;
    for (NSString *item in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge_retained CFStringRef)item);
        if (info&&[info count]) {
            break;
        }
    }
    return [info copy];
}
//
//+(NSString*)getCurrentSSID {
//    
//    NSDictionary * ssidDict = [self fetchSSIDInfo];
//    if (ssidDict) {
//        
//        
//        return [ssidDict objectForKey:(__bridge_transfer NSString*)kCNNetworkInfoKeySSID];
//    }
//	return @"未连接";
//}
//
//+(UIView*) showTips:(NSString *)message atView:(UIView*)view {
//    #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0)
//    
//    AMPopTip *pop =[AMPopTip popTip];
//    pop.popoverColor = [UIColor colorWithRed:214.0/255 green:31.0/255 blue:42.0/255 alpha:1];
//    pop.textColor = [UIColor whiteColor];
////    pop.shouldDismissOnTapOutside = YES;
////    pop.shouldDismissOnTap = YES;
//    
//    [pop showText:message direction:AMPopTipDirectionDown maxWidth:view.superview.frame.size.width-10 inView:view.superview fromFrame:view.frame];
//    return pop;
//    
//    #else
//    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//    [alert show];
//    return alert;
//    #endif
//	
//}
//
//+(void) hideTip:(UIView*)tip {
//    
//#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0)
//    if ([tip respondsToSelector:@selector(hide)]) {
//        [tip performSelector:@selector(hide)];
//    }
//#endif
//}
+(void)showToastView:(NSString *)message onView:(UIView *)view
{
    __block MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:HUD];
    HUD.labelText = message;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的位置，不指定则在屏幕中间显示
    //    HUD.yOffset = 100.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        //        [HUD release];
        HUD = nil;
    }];
}




@end
